package robuin.ioio.config;

//import robuin.ioio.config.IOIOSeek.SlowAdapter;



import java.util.ArrayList;
import java.util.List;

import robuin.ioio.config.R;
import ioio.lib.api.DigitalOutput;
import ioio.lib.api.exception.ConnectionLostException;
import ioio.lib.util.android.IOIOActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.ToggleButton;
import ioio.lib.util.BaseIOIOLooper;
import ioio.lib.util.IOIOLooper;

	public class digitalOutput extends IOIOActivity{
		List<ToggleButton> listToggleButtons_ = new ArrayList<ToggleButton>();
	    private TextView mStatus;


	    @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);

	        Intent intent = getIntent();
	        String message = intent.getStringExtra(IOIOConfig.EXTRA_MESSAGE);
	        
	        setContentView(R.layout.digitaloutput);

	        listToggleButtons_.add((ToggleButton)findViewById(R.id.ToggleButton01));
	        listToggleButtons_.add((ToggleButton)findViewById(R.id.ToggleButton02));
	        listToggleButtons_.add((ToggleButton)findViewById(R.id.ToggleButton03));
	        listToggleButtons_.add((ToggleButton)findViewById(R.id.ToggleButton04));
	        listToggleButtons_.add((ToggleButton)findViewById(R.id.ToggleButton05));
	        listToggleButtons_.add((ToggleButton)findViewById(R.id.ToggleButton06));
	        listToggleButtons_.add((ToggleButton)findViewById(R.id.ToggleButton07));
	        listToggleButtons_.add((ToggleButton)findViewById(R.id.ToggleButton08));
	        listToggleButtons_.add((ToggleButton)findViewById(R.id.ToggleButton09));
	        listToggleButtons_.add((ToggleButton)findViewById(R.id.ToggleButton10));
	        listToggleButtons_.add((ToggleButton)findViewById(R.id.ToggleButton11));
	        listToggleButtons_.add((ToggleButton)findViewById(R.id.ToggleButton12));
	        listToggleButtons_.add((ToggleButton)findViewById(R.id.ToggleButton13));
	        listToggleButtons_.add((ToggleButton)findViewById(R.id.ToggleButton14));
	        listToggleButtons_.add((ToggleButton)findViewById(R.id.ToggleButton15));
	        listToggleButtons_.add((ToggleButton)findViewById(R.id.ToggleButton16));
	        listToggleButtons_.add((ToggleButton)findViewById(R.id.ToggleButton17));
	        listToggleButtons_.add((ToggleButton)findViewById(R.id.ToggleButton18));
	        listToggleButtons_.add((ToggleButton)findViewById(R.id.ToggleButton19));
	        listToggleButtons_.add((ToggleButton)findViewById(R.id.ToggleButton20));
	        listToggleButtons_.add((ToggleButton)findViewById(R.id.ToggleButton21));
	        listToggleButtons_.add((ToggleButton)findViewById(R.id.ToggleButton22));
	        listToggleButtons_.add((ToggleButton)findViewById(R.id.ToggleButton23));
	        listToggleButtons_.add((ToggleButton)findViewById(R.id.ToggleButton24));
	        listToggleButtons_.add((ToggleButton)findViewById(R.id.ToggleButton25));
	        listToggleButtons_.add((ToggleButton)findViewById(R.id.ToggleButton26));
	        listToggleButtons_.add((ToggleButton)findViewById(R.id.ToggleButton27));
	        listToggleButtons_.add((ToggleButton)findViewById(R.id.ToggleButton28));
	        listToggleButtons_.add((ToggleButton)findViewById(R.id.ToggleButton29));
	        listToggleButtons_.add((ToggleButton)findViewById(R.id.ToggleButton30));
	        listToggleButtons_.add((ToggleButton)findViewById(R.id.ToggleButton31));
	        listToggleButtons_.add((ToggleButton)findViewById(R.id.ToggleButton32));
	        listToggleButtons_.add((ToggleButton)findViewById(R.id.ToggleButton33));
	        listToggleButtons_.add((ToggleButton)findViewById(R.id.ToggleButton34));
	        listToggleButtons_.add((ToggleButton)findViewById(R.id.ToggleButton35));
	        listToggleButtons_.add((ToggleButton)findViewById(R.id.ToggleButton36));
	        listToggleButtons_.add((ToggleButton)findViewById(R.id.ToggleButton37));
	        listToggleButtons_.add((ToggleButton)findViewById(R.id.ToggleButton38));
	        listToggleButtons_.add((ToggleButton)findViewById(R.id.ToggleButton39));
	        listToggleButtons_.add((ToggleButton)findViewById(R.id.ToggleButton40));
	        listToggleButtons_.add((ToggleButton)findViewById(R.id.ToggleButton41));
	        listToggleButtons_.add((ToggleButton)findViewById(R.id.ToggleButton42));
	        listToggleButtons_.add((ToggleButton)findViewById(R.id.ToggleButton43));
	        listToggleButtons_.add((ToggleButton)findViewById(R.id.ToggleButton44));
	        listToggleButtons_.add((ToggleButton)findViewById(R.id.ToggleButton45));
	        listToggleButtons_.add((ToggleButton)findViewById(R.id.ToggleButton46));
	        listToggleButtons_.add((ToggleButton)findViewById(R.id.ToggleButton47));
	        listToggleButtons_.add((ToggleButton)findViewById(R.id.ToggleButton48));
	        
	        mStatus = (TextView) findViewById(R.id.status1);
	        mStatus.setText(message);

	        enableUi(false);
	    }
	    
	    class IOIOThread extends BaseIOIOLooper {
	    	List<DigitalOutput> listDigitalOutput_ = new ArrayList<DigitalOutput>();

	    	@Override
			public void setup() throws ConnectionLostException {
				try {
					for(int i = 1; i <= 48; i++)
						listDigitalOutput_.add(ioio_.openDigitalOutput(i, false));
					
					enableUi(true);
				} catch (ConnectionLostException e) {
					enableUi(false);
					throw e;
				}
			}
			
			@Override
			public void loop() throws ConnectionLostException {
				try {
					for(int i = 0; i < listDigitalOutput_.size(); i++)
						listDigitalOutput_.get(i).write(listToggleButtons_.get(i).isChecked());
					
					Thread.sleep(100);
				} catch (InterruptedException e) {
					ioio_.disconnect();
				} catch (ConnectionLostException e) {
					enableUi(false);
					throw e;
				}
			}
			
			@Override
	    	public void disconnected(){
	    		for(int i = 0; i < listDigitalOutput_.size(); i++)
	    			listDigitalOutput_.get(i).close();
	    	}
		}
	    
	    /**
		 * A method to create our IOIO thread.
		 */
		@Override
		protected IOIOLooper createIOIOLooper() {
			return new IOIOThread();
		}
	    
		private void enableUi(final boolean enable) {
			runOnUiThread(new Runnable() {
				@Override
				public void run() {
					for(int i = 0; i < 48; i++)
						listToggleButtons_.get(i).setEnabled(enable);
				}
			});
		}
				
		
	}